﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using RSSFeedReader.Services;

namespace RSSFeedReader.ViewModels
{
    /// <summary>
    /// Base class for all ViewModel classes in the application.
    /// It provides support for property change notifications.
    /// This class is abstract.
    /// </summary>
    public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
    {
        #region Constructor
        /// <summary>
        /// Initialize a new instance of the ViewModelBase class.
        /// </summary>
        [DebuggerStepThrough()]
        public ViewModelBase()
        {
        }
        #endregion

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a change is made to a property.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Fires the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The name of the property that changed.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region RequestClose Event Members
        /// <summary>
        /// Event fired when the <see cref="RSSFeedReader.ViewModels"/> 
        /// attempts to close.
        /// </summary>
        public event EventHandler RequestClose;

        /// <summary>
        /// Handles the request close event.
        /// </summary>
        public void OnRequestClose()
        {
            EventHandler handler = RequestClose;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }
        #endregion

        #region Services
        /// <summary>
        /// Gets the ILoggerService.
        /// </summary>
        public ILoggerService Logger { get { return Application.Current as ILoggerService; } }

        /// <summary>
        /// Gets the IDisplayMessageService.
        /// </summary>
        public IDisplayMessageService DisplayMessage { get { return Application.Current as IDisplayMessageService; } }

        /// <summary>
        /// Gets the IDisplayWindowService.
        /// </summary>
        public IDisplayWindowService DisplayWindow { get { return Application.Current as IDisplayWindowService; } }
        #endregion

        #region IDisposable Members

        /// <summary>
        /// Invoked when this object is being removed from the application
        /// and will be subject to garbage collection.
        /// </summary>
        public void Dispose()
        {
            OnDispose();
        }

        /// <summary>
        /// Child classes can override this method to perform 
        /// clean-up logic, such as removing event handlers.
        /// </summary>
        protected virtual void OnDispose()
        {
        }

        #endregion
    }
}
